package controller;

import DAO.BaseDAO;
import DAO.CategoryDAO;
import DAO.MetricAdminDAO;
import DAO.MetricDAO;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;
import model.Category;
import model.Metric;
import model.Metric_User;
import model.User;
import org.primefaces.context.RequestContext;

@ManagedBean(name = "registerController")
@SessionScoped
public class MetricController {

    public Metric_User metric;
    private Metric metricAdmin;
    public List lista;
    public DataModel listMetrics;
    private DataModel listAdminMetrics;
    FacesContext fc = FacesContext.getCurrentInstance();
    HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
    User u = (User) session.getAttribute("user");

    public MetricController() {
        metric = new Metric_User();
    }

    public List<SelectItem> getCategory() {
        BaseDAO pessoaService = new CategoryDAO();
        List<Category> category = pessoaService.list();
        List<SelectItem> itens = new ArrayList<SelectItem>(category.size());
        for (Category p : category) {
            itens.add(new SelectItem(p.getCategory_id(), p.getName()));
        }
        return itens;
    }

    public boolean getAnyCategorySelected() {
        return metric.getCategory().getCategory_id() > 0;
    }

    public List<SelectItem> getMetricList() {
        MetricDAO metricDAO = new MetricDAO();
        List<Metric_User> metric2 = metricDAO.glist(metric.getCategory().getCategory_id());
        List<SelectItem> itens = new ArrayList<SelectItem>(metric2.size());
        for (Metric_User m : metric2) {
            itens.add(new SelectItem(m.getMetric_id(), m.getName()));
        }
        return itens;
    }

    public Metric_User getMetric() {
        return metric;
    }

    public void setMetric(Metric_User metric) {
        this.metric = metric;
    }

    public String addMetric() {
        metric = new Metric_User();
        return "registerMetric";
    }

    public void saveMetric() throws Exception
    {
        String localFile = (String) session.getAttribute("localFile");
        XmlReader xml = new XmlReader(localFile);
        if(u.getRole()==1)
        {
            List<Metric> mA = xml.lerMetricAdmin();
            BaseDAO dao = new MetricAdminDAO();
            for(Metric m: mA){
                m.setCategory(metric.getCategory());
                dao.save(m);
            }
         setMetricAdmin(new Metric());
        }else{
            List<Metric_User> m = xml.lerMetric();
            BaseDAO dao = new MetricDAO();
            for(Metric_User me: m)
            {
                    me.setUserid(u.getId());
                    me.setCategory(metric.getCategory());
                    dao.save(me); 
            }
            File f = new File("/home/odiar/Public/file/use_definition/",String.valueOf(u.getId()) + ".XML");
            f.renameTo(new File("/home/odiar/Public/file/use_definition/", String.valueOf(u.getId()) + m.get(0).getCategory().getCategory_id() +".XML"));
            setMetric(new Metric_User());
        }
    }

    public DataModel getListMetrics() {
        List<Metric_User> list = new MetricDAO().list();
        listMetrics = new ListDataModel(list);
        return listMetrics;
    }

    public DataModel getListMetricAdmin() {
        List<Metric> list = new MetricAdminDAO().list();
        listAdminMetrics = new ListDataModel(list);
        return listAdminMetrics;
    }

    public String prepareUpdate() {
        metric = (Metric_User) (listMetrics.getRowData());
        return "updateMetric";
    }

    public String prepareUpdateAdmin() {
        metricAdmin = (Metric) (listAdminMetrics.getRowData());
        return "updateMetricAdmin";
    }

    public String updateMetric() {
        if (u.getRole() == 1) {
            BaseDAO dao = new MetricAdminDAO();
            dao.update(metricAdmin);
            setMetricAdmin(new Metric());
            return "consultMetricAdmin";
        } else {
            BaseDAO dao = new MetricDAO();
            dao.update(metric);
            setMetric(new Metric_User());
            return "consultMetricAdmin";
        }
    }

    public String removeMetric() {

        if (u.getRole() == 1) {
            Metric metricTemp = (Metric) (listAdminMetrics.getRowData());
            BaseDAO dao = new MetricAdminDAO();
            dao.remove(metricTemp);
            return "#";
        } else {
            Metric_User metricTemp = (Metric_User) (listMetrics.getRowData());
            BaseDAO dao = new MetricDAO();
            dao.remove(metricTemp);
            return "#";
        }
    }

    public void clearMetric() {
        setMetric(new Metric_User());
    }

    /**
     * @return the metricAdmin
     */
    public Metric getMetricAdmin() {
        return metricAdmin;
    }

    /**
     * @param metricAdmin the metricAdmin to set
     */
    public void setMetricAdmin(Metric metricAdmin) {
        this.metricAdmin = metricAdmin;
    }

    /**
     * @return the listAdminMetrics
     */
    public DataModel getListAdminMetrics() {
        return listAdminMetrics;
    }

    /**
     * @param listAdminMetrics the listAdminMetrics to set
     */
    public void setListAdminMetrics(DataModel listAdminMetrics) {
        this.listAdminMetrics = listAdminMetrics;
    }
}
